<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/6/4
 * Time: 15:50
 */

namespace common\components\extenders;

use yii\base\Component;
use yii\httpclient\Client;
use yii\helpers\Json;

class RestManage extends Component
{
	
	public $total_backend_url='http://localhost/ad/index.php?r=api/';

    public $square_backend_url='http://aks.yijiasan.com/api-';

    //public $web_backend_url = 'http://admin.doumiaoduo.com/index.php?r=api';

    public $web_backend_url;

    function init(){
        parent::init();
        $this->web_backend_url=\Yii::$app->params['http_system_api_rest_url'];
    }

    /*
     * httpclient GET 请求
     * $url:API地址
     *
     * */
    protected function httpGet($url)
    {
        /*$client = new Client();
        return $client->get($url)->send();*/
        $client = new Client();
        $response = $client->createRequest()
            ->setMethod('get')
            ->setUrl($url)
            ->addHeaders(['content-type' => 'application/json'])
            ->send();
        $rest_data = Json::decode($response->content);
        return $rest_data;

    }

    /*
     * httpclient POST 请求
     * $url:API地址
     * $data:POST提交的数据
     * */
    protected function httpPost($url, $data)
    {
        $client = new Client();
        $response = $client->createRequest()
            ->setMethod('post')
            ->setUrl($url)
            ->addHeaders(['content-type' => 'application/json'])
            ->setData($data)
            ->send();
        return $response->content;
    }

    /*
     * httpclient PUT 请求
     * $url:API地址
     * $data:POST提交的数据
     * */
    protected function httpPut($url, $data)
    {
        $client = new Client();
        $response = $client->createRequest()
            ->setMethod('put')
            ->setUrl($url)
            ->addHeaders(['content-type' => 'application/json'])
            ->setData($data)
            ->send();
        return $response->content;
    }

    /*
     * httpclient DELETE 请求
     * $url:API地址
     * $data:POST提交的数据
     * */
    protected function httpDelete($url, $data)
    {
        $client = new Client();
        $response = $client->createRequest()
            ->setMethod('delete')
            ->setUrl($url)
            ->addHeaders(['content-type' => 'application/json'])
            ->setData($data)
            ->send();
        return $response->content;
    }

    //获取网站城市信息
    public function listWebsite()
    {
        $url = $this->web_backend_url . '/website/index';
        return $this->httpGet($url);
    }

    public function listWechat()
    {
        $url = $this->web_backend_url . '/wechat/index';
        return $this->httpGet($url);
    }
    //总后台产品分类数据插入后网站数据同步
    public function createCategory($data)
    {
        $url = $this->web_backend_url . '/category/create';
        return $this->httpPost($url, $data);
    }

    //总后台产品分类数据修改后网站数据同步
    public function updateCategory($data)
    {
        $url = $this->web_backend_url . '/category/update';
        return $this->httpPut($url, $data);
    }

    //总后台产品分类数据删除后网站数据同步
    public function deleteCategory($data)
    {
        $url = $this->web_backend_url . '/category/delete';
        return $this->httpDelete($url, $data);
    }

    //总后台产品类型数据插入后网站数据同步
    public function createType($data)
    {
        $url = $this->web_backend_url . '/type/create';
        return $this->httpPost($url, $data);
    }

    //总后台产品类型数据修改后网站数据同步
    public function updateType($data)
    {
        $url = $this->web_backend_url . '/type/update';
        return $this->httpPut($url, $data);
    }

    //总后台产品类型数据删除后网站数据同步
    public function deleteType($data)
    {
        $url = $this->web_backend_url . '/type/delete';
        return $this->httpDelete($url, $data);
    }

    //总后台产品类型属性数据插入后网站数据同步
    public function createTypeAttr($data)
    {
        $url = $this->web_backend_url . '/typeattr/create';
        return $this->httpPost($url, $data);
    }

    //总后台产品类型属性数据修改后网站数据同步
    public function updateTypeAttr($data)
    {
        $url = $this->web_backend_url . '/typeattr/update';
        return $this->httpPut($url, $data);
    }

    //总后台产品类型属性数据删除后网站数据同步
    public function deleteTypeAttr($data)
    {
        $url = $this->web_backend_url . '/typeattr/delete';
        return $this->httpDelete($url, $data);
    }

    public function createWebSales($data)
    {
        $url = $this->web_backend_url . '/sales/create';
        return $this->httpPost($url, $data);
    }

    public function deleteWebSales($data){
        $url = $this->web_backend_url . '/sales/delete';
        return $this->httpDelete($url, $data);
    }

    public function updateWebSales($data){
        $url = $this->web_backend_url . '/sales/update';
        return $this->httpPut($url, $data);
    }

    public function createWebSalesAttr($data)
    {
        $url = $this->web_backend_url . '/salesattr/create';
        return $this->httpPost($url, $data);
    }

    public function updateWebSalesAttr($data)
    {
        $url = $this->web_backend_url . '/salesattr/update';
        return $this->httpPut($url, $data);
    }

    public function deleteWebSalesAttr($data)
    {
        $url = $this->web_backend_url . '/salesattr/delete';
        return $this->httpPost($url, $data);
    }

    public function createWebSalesImages($data)
    {
        $url = $this->web_backend_url . '/salesimages/create';
        return $this->httpPost($url, $data);
    }

    public function updateWebSalesImages($data)
    {
        $url = $this->web_backend_url . '/salesimages/update';
        return $this->httpPut($url, $data);
    }

    public function deleteWebSalesImages($data)
    {
        $url = $this->web_backend_url . '/salesimages/delete';
        return $this->httpPost($url, $data);
    }

    public function createWebSiteCache($data)
    {
        $url = $this->web_backend_url . '/sitecache/create';
        return $this->httpPost($url, $data);
    }

    public function updateWebSiteCache($data)
    {
        $url = $this->web_backend_url . '/sitecache/update';
        return $this->httpPut($url, $data);
    }

    public function createCustomer($data)
    {
        $url = $this->web_backend_url . '/customer/create';
        return $this->httpPost($url, $data);
    }

    public function updateCustomer($data)
    {
        $url = $this->web_backend_url . '/customer/update';
        return $this->httpPut($url, $data);
    }

    public function createMessage($data){
        $url=$this->web_backend_url. '/message/create';
        return $this->httpPost($url,$data);
    }

    public function createOrder($data)
    {
        $url = $this->web_backend_url . '/order/create';
        return $this->httpPost($url, $data);
    }
    public function createOrderSales($data)
    {
        $url = $this->web_backend_url . '/ordersales/create';
        return $this->httpPost($url, $data);
    }
    public function updateOrderSales($data){
        $url=$this->web_backend_url. '/ordersales/update';
        return $this->httpPut($url,$data);
    }
	
	public function getSite(){
        $url=$this->total_backend_url.'site';
        return $this->httpGet($url);
    }
	
	public function getVirtualCard(){
        $url=$this->total_backend_url.'virtual';
        return $this->httpGet($url);
    }
	
	public function getBatch(){
        $url=$this->total_backend_url.'batch';
        return $this->httpGet($url);
    }
	
	public function getCardAttr(){
        $url=$this->total_backend_url.'cardattr';
        return $this->httpGet($url);
    }
	
	public function getPta(){
        $url=$this->square_backend_url.'pta';
        return $this->httpGet($url);
    }
	
	public function getPtaActive(){
        $url=$this->square_backend_url.'ptaactive';
        return $this->httpGet($url);
    }
	
	public function getPacket($data){
        $url=$this->total_backend_url.'packet/update';
        return $this->httpPut($url,$data);
    }
	
	public function getCustomerInsert($data){
        $url=$this->total_backend_url.'customer/insert';
        return $this->httpPost($url,$data);
    }
	
	public function getCustomerUpdate($data){
        $url=$this->total_backend_url.'customer/update';
        return  $this->httpPut($url,$data);
    }
	
	public function getCustomerDelete($data){
        $url=$this->total_backend_url.'customer/delete';
        return  $this->httpDelete($url,$data);
    }
	
	public function getPtaInsert($data){
        $url=$this->total_backend_url.'pta/insert';
        return $this->httpPost($url,$data);
    }
	
	public function getPtaProposalInsert($data){
        $url=$this->square_backend_url.'ptaproposal-insert';
        return $this->httpPost($url,$data);
    }
	
	public function updateWechatCache($data)
    {
        $url = $this->total_backend_url . '/wechatcache/update';
        return $this->httpPut($url, $data);
    }

}